package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsPermissionMapper;
import com.qf.mapper.DtsRoleMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsPermission;
import com.qf.pojo.DtsRole;
import com.qf.service.DtsRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 千锋教育
 * @Company http://www.mobiletrain.org/
 * @Version 1.0
 */
@Service
public class DtsRoleServiceImpl implements DtsRoleService {

    @Autowired
    DtsRoleMapper roleMapper;

    @Override
    public Set<String> findRoleByIds(Integer[] roleIds) {
        LambdaQueryWrapper<DtsRole> qw = new LambdaQueryWrapper<>();
        qw.in(DtsRole::getId, roleIds);
        List<DtsRole> roles = roleMapper.selectList(qw);

        //stream 转换为流， map： 要映射成一个什么样的对象  collect：指定返回的集合类型
        Set<String> roleStrs = roles.stream().map(role -> {
            return role.getName();
        }).collect(Collectors.toSet());

        return roleStrs;
    }

    @Override
    public List<DtsRole> findAll() {
        return roleMapper.selectList(null);
    }

    @Override
    public IPage<DtsRole> queryPage(Integer page, Integer limit, String sort, String order, String rolename) {
        IPage<DtsRole> ipage=new Page<>(page,limit);
        QueryWrapper qw=new QueryWrapper<>();
        if(!StringUtils.isEmpty(rolename)){
            qw.like("name",rolename);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equals("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        roleMapper.selectPage(ipage,qw);
        return ipage;
    }

    @Override
    public DtsRole add(DtsRole dtsRole) {

        dtsRole.setEnabled(true);
        dtsRole.setAddTime(new Date());
        dtsRole.setUpdateTime(new Date());
        dtsRole.setDeleted(false);
        try {
            roleMapper.insert(dtsRole);
        } catch (Exception e) {
            throw new RuntimeException("新增失败:角色已存在");
        }
        return dtsRole;
    }

    @Override
    public DtsRole update(DtsRole role) {
        try {
            roleMapper.updateById(role);
        } catch (Exception e) {
            throw new RuntimeException("修改失败:角色已存在");
        }
        return null;
    }

    @Autowired
    DtsAdminMapper adminMapper;
    @Autowired
    DtsPermissionMapper permissionMapper;
    @Override
    public boolean delete(DtsRole role) {
        //如果该角色被管理员使用，则不能删除
        LambdaQueryWrapper<DtsAdmin> qw1=new LambdaQueryWrapper<>();
        qw1.select(DtsAdmin::getRoleIds);
        List<DtsAdmin> adminList = adminMapper.selectList(qw1);
        for (DtsAdmin admin:adminList){
            Integer[] roleIds=admin.getRoleIds();
            for (Integer roleId : roleIds) {
                if(roleId.equals(role.getId())){
                    return false;
                }
            }
         }

        //如果该角色关联了权限，则不能删除
        LambdaQueryWrapper<DtsPermission> qw2=new LambdaQueryWrapper<>();
        qw2.eq(DtsPermission::getRoleId,role.getId());
        Integer count = permissionMapper.selectCount(qw2);
        if(count>=1){
            return false;
        }
        //如果没有被管理员和权限关联，则删除
        roleMapper.deleteById(role.getId());
        return true;

    }
}
